package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 *
 * @author axi
 *
 */
public class XCarBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.xcar.com.cn/bbs/viewthread.php[?]tid=[0-9]+",
				"http://www.xcar.com.cn/bbs/viewthread.php[?]page=1&tid=[0-9]+",
				//http://www.xcar.com.cn/bbs/viewthread.php?tid=19558599
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta bbs = new NewsMeta();
		String title = "";
		String content = "";
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;

		/**
		 * 标题
		 */
		title = doc.select("h1").text().replace("标题: ", "");

		String author = null;
		Elements select2 = doc.select("td.t_user a.bold[href*=http://my.xcar.com.cn/space.php?uid=]");
		Element first = select2.first();
		if(first!=null){
			author = first.text();
		}
		/**
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("html body div div#mainNew div.maintable div#_img.maintable form#delpost div.F_box_2 table.t_row tbody tr td table.t_msg tbody tr td div div").text());
		/**
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url,  doc.select("#delpost  table.t_msg td.line div.t_msgfont1"));
		/**
		 * 作者
		 */
		if (author==null){
			if(doc.select("a.bold").size() > 0){
				author = doc.select("a.bold").get(0).text();
			}
		}

		/**
		 * 查看，评论
		 */
		Elements select = doc.select("html body div div#mainNew div.maintable div#_img.maintable div.F_box_2 table tbody tr td.titleStyle3 p#showPic span");
		if(select.size() > 0){
			String matchDate = select.toString();
			Pattern clickReg = Pattern.compile("查看：[0-9]*");
			Matcher matcher_click = clickReg.matcher(matchDate);
			if (matcher_click.find()) {
				 String click = matcher_click.group();
				 if(click != null){
					 click = click.replace("查看：", "");
					 try {
						clickNum = Integer.parseInt(click);
					} catch (NumberFormatException e) {
						clickNum = 0;
					}
				 }
			}
			Pattern commentReg = Pattern.compile("回复：[0-9]*");
			Matcher matcher_comment = commentReg.matcher(matchDate);
			if (matcher_comment.find()) {
				 String comment = matcher_comment.group();
				 if(comment != null){
					 comment = comment.replace("回复：", "");
					 try {
						commentNum = Integer.parseInt(comment);
					} catch (NumberFormatException e) {
						commentNum = 0;
					}
				 }
			}
			bbs.setUpdateUrl(url);
		}
		if(StringUtils.isBlank(title)){
		}
		bbs.setUrl(url);
		bbs.setTitle(title);
		bbs.setAuthor(author);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setType(4);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta != null){
			String url = meta.getUpdateUrl();
			if(!StringUtils.isBlank(url)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(url);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	/**
	 * @param args
	 */
	

	
}
